package Yunzai

import (
	"fmt"
	"GoBox/src/Node"
	"GoBox/src/Scripts"
	"time"
	"os"
	"os/exec"
	"runtime"
	"bufio"
)
const (
	Red = "\033[31m Error: "
	Green = "\033[32m Success:"
	Yellow = "\033[33m Warning:"
	Reset = "\033[0m"
)


func YunZaiBot_init(GoBoxPath,BotPath, NodePath, FFmpegPath string) {
	//启动redis 
	if err := Node.NodeCommand(GoBoxPath,BotPath, NodePath, FFmpegPath); err != nil {
		fmt.Println("\033[31m 喵崽/云崽启动失败,请检查是否缺失依赖,或者Node环境...", err, "\033[0m")
		//打印yunzai
		fmt.Println("BotPath:",BotPath)
		//切换GoBox目录
		// os.Chdir(GoBoxPath)
	}
	time.Sleep(4 * time.Second)
}

// 2.主人账号配置
func YunZaiBot_master_config(GoBoxPath,BotPath string) {
	var alemon_bot_menu = `
	===========主人账户配置===========
	  1.添加主人账户
	  2.删除主人账户
	  3.查看所有主人账户
	  0.返回主菜单
	请输入菜单编号：`
	for {
		var choice int
		clearLog()
		fmt.Printf("\033[34m")
		fmt.Print(alemon_bot_menu)
		fmt.Scanln(&choice)
		switch choice {
		case 1:
			// 1.添加主人账户
			YunZaiBot_master_add(GoBoxPath,BotPath)
		case 2:
			// 2.修改主人账户
			YunZaiBot_master_del(GoBoxPath,BotPath)
		case 3:
			YunZaiBot_master_list(GoBoxPath,BotPath)
		case 0:
			// 返回主菜单
			return
		default:
			//红色
			fmt.Println(Red,"输入错误，请重新输入",Reset)
			reader := bufio.NewReader(os.Stdin)
			// 清除缓存区中的回车符
			_, _ = reader.ReadBytes('\n')
		}
	}

}

// 高级选项
func YunZaiBot_advanced(GoBoxPath,BotPath string) {
	var YunZaiBot_advanced = `
	=============高级选项=============
	  1.重新生成设备文件
	  2.一键配置API(需要Docker)
	  0.返回主菜单
	请输入菜单编号：`
	var choice int
	for {
		clearLog()
		fmt.Printf("\033[34m")
		fmt.Printf(YunZaiBot_advanced)
		fmt.Scanln(&choice)
		switch choice {
		case 1:
			// 1.重新生成设备文件
			Up_Device(GoBoxPath,BotPath)
		case 2:
			// 2.安装API
			Scripts.RUN_main(GoBoxPath,"Api.sh",BotPath)
		case 0:
			// 返回主菜单
			return
		default:
			fmt.Println("输入错误，请重新输入")
		}
	}
}


func clearLog() {
	var cmd *exec.Cmd
	if runtime.GOOS == "windows" {
		cmd = exec.Command("cmd", "/c", "cls")
	} else {
		cmd = exec.Command("clear")
	}
	cmd.Stdout = os.Stdout
	cmd.Run()
}
